package com.healthfitness.floating

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.healthfitness.floating.service.FloatingService
import com.healthfitness.floating.ui.theme.FloatingDemoTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            FloatingDemoTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    Greeting(
                        name = "me",
                        modifier = Modifier.padding(innerPadding),
                        onStartApp = { name ->
                            startApp(name)
                        }
                    ) {
                        if (checkFloatPermission()) {
                            startService(Intent(this, FloatingService::class.java))
                            moveTaskToBack(true)
                        } else {
                            requestFloatPermission()
                        }
                    }
                }
            }
        }
    }

    private fun startApp(name: String) {
        when (name) {
            "prime" -> {
                launchApp("com.amazon.avod.thirdpartyclient")
            }

            "netflix" -> {
                launchApp("com.netflix.mediaclient")
            }

            "spotify" -> {
                launchApp("com.spotify.music")
            }

            "hulu" -> {
                launchApp("jp.happyon.android")
            }

        }
    }


    private fun launchApp(packageName:String){
       val intent =  packageManager.getLaunchIntentForPackage(packageName)
        if(intent!=null){
            startActivity(intent)
        }else{
            Log.d("linlian"," launchApp no $packageName")
        }
    }

    private fun checkFloatPermission(): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Settings.canDrawOverlays(this)
        } else {
            true
        }
    }

    private fun requestFloatPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
                data = Uri.parse("package:$packageName")
            }
            startActivityForResult(intent, 100)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && checkFloatPermission()) {
            startService(Intent(this, FloatingService::class.java))
            moveTaskToBack(true)
        }
    }
}

@Composable
fun Greeting(
    modifier: Modifier = Modifier,
    name: String,
    onStartApp: (String) -> Unit = {},
    onFloating: () -> Unit = {}
) {
    Column(modifier = modifier) {
        Text(
            modifier = Modifier.clickable {

                onFloating.invoke()
            },
            text = "Floating $name!",
        )

        Text(text = "prime", modifier = Modifier.clickable {
            onStartApp.invoke("prime")
        })

        Text(text = "netflix", modifier = Modifier.clickable {
            onStartApp.invoke("netflix")
        })

        Text(text = "spotify", modifier = Modifier.clickable {
            onStartApp.invoke("spotify")
        })

        Text(text = "hulu", modifier = Modifier.clickable {
            onStartApp.invoke("hulu")
        })

    }

}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    FloatingDemoTheme {
        Greeting(name = "Android")
    }
}